from rest_framework import serializers
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer


class LoginSerializer(TokenObtainPairSerializer):
    @classmethod
    def get_token(cls, user):
        token = super().get_token(user)

        # 可以添加任何额外的信息到token中
        token['username'] = user.username  # 例如，添加用户名到token中
        token['email'] = user.email  # 例如，添加邮箱到token中

        return token

    def validate(self, attrs):
        data = super().validate(attrs)
        refresh = self.get_token(self.user)
        data['refresh'] = str(refresh)
        data['access'] = str(refresh.access_token)
        data['username'] = self.user.username  # 直接在响应中添加用户名等额外信息
        data['email'] = self.user.email  # 直接在响应中添加邮箱等额外信息
        return data
